﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sinbiota.Web.Models;

namespace Sinbiota.Web.Extensions
{
    public static class ListExtensions
    {
        public static IEnumerable<SelectListItem> ToSelectListItems<T>(this IEnumerable<T> items, Func<T, string> nameSelector,Func<T, string> valueSelector, Func<T, bool> selected)
        {
            return items.OrderBy(nameSelector)
                   .Select(item =>
                           new SelectListItem
                           {
                               Selected = selected(item),
                               Text = nameSelector(item),
                               Value = valueSelector(item)
                           });
        }

        public static IEnumerable<CheckedItem> ToCheckedItems<T>(this IEnumerable<T> items, Func<T, string> nameSelector, Func<T, int> valueSelector, Func<T, bool> selected)
        {
            return items.OrderBy(nameSelector).Select(item => new CheckedItem(valueSelector(item), selected(item), nameSelector(item)));
        }

    }
}